<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.util.Map" %>
<%@ page import="JEE2.Connector" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="static com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.println" %>
<jsp:useBean id="dataB" class="JEE2.Bean" scope="session"></jsp:useBean>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>



  <head>
    <title>$Title$</title>
  </head>
  <body>



  <%
    Map map = new HashMap();
    try {
      Connector con = new Connector();
      map = con.query("select * from onSale;");
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    String[][] data = new String[map.size()][6];
    for(int i=0; i<map.size(); i++){
      Map map0 = (Map) map.get(""+i);
      String[] fields = {"id","goods","type","describe0","amount","price"};
      for(int j=0; j<6; j++){
        data[i][j] = (String)map0.get(fields[j]);
      }
      dataB.setData(data);
      System.out.println(data[0][1]);
    }
  %>

  <script>
    function choose1() {
      <%dataB.setCount(0);%>
      <%dataB.setGoods(dataB.getData()[dataB.getCount()][1]);%>
      <%dataB.setPrice(dataB.getData()[dataB.getCount()][5]);%>
      location.reload();
    }
    function choose2() {
      <%dataB.setCount(1);%>
      <%dataB.setGoods(dataB.getData()[dataB.getCount()][1]);%>
      <%dataB.setPrice(dataB.getData()[dataB.getCount()][5]);%>
      location.reload();
    }
    function choose3() {
      <%dataB.setCount(2);%>
      <%dataB.setGoods(dataB.getData()[dataB.getCount()][1]);%>
      <%dataB.setPrice(dataB.getData()[dataB.getCount()][5]);%>
      location.reload();
    }
  </script>

  <table>
    <tr>
      <td>商品</td>
      <td>类型</td>
      <td>描述</td>
      <td>存货</td>
      <td>价格</td>
    </tr>
    <tr>
      <td><%=data[0][1] %></td>
      <td><%=data[0][2] %></td>
      <td><%=data[0][3] %></td>
      <td><%=data[0][4] %></td>
      <td><%=data[0][5] %></td>
      <td><button type="button" id="b1" onclick="choose1()">选购</button></td>
    </tr>
    <tr>
      <td><%=data[1][1] %></td>
      <td><%=data[1][2] %></td>
      <td><%=data[1][3] %></td>
      <td><%=data[1][4] %></td>
      <td><%=data[1][5] %></td>
      <td><button type="button" id="b2" onclick="choose2()">选购</button></td>
    </tr>
    <tr>
      <td><%=data[2][1] %></td>
      <td><%=data[2][2] %></td>
      <td><%=data[2][3] %></td>
      <td><%=data[2][4] %></td>
      <td><%=data[2][5] %></td>
      <td><button type="button" id="b3" onclick="choose3()">选购</button></td>
    </tr>
  </table>

  <form action="JEE2.DBM" method="get">
    <button type="submit">就这些</button> &nbsp;&nbsp;
  </form>
  <br>
  <br><br>
  购物车：
  <textarea>${dataB.getGoods()}  </textarea >
  <%
    int sum = 0;
    for(int i=0; i<dataB.getGoods().length; i++){
      dataB.setCount(i);
      dataB.setPrice("0");
      println(dataB.getGoods(i)+'\t'+dataB.getPrice(i));
      sum += Integer.parseInt(dataB.getPrice(i));
      dataB.setSum(sum);
    }
  %>
  总价：
  <%
  println(""+dataB.getSum());
  %>

  </body>
</html>
